//
//  OPHomeUserLoginBtn.m
//  OpenPractice
//
//  Created by tanyuehong on 2021/10/20.
//  Copyright © 2021 tanyuehong. All rights reserved.
//

#import <Masonry/Masonry.h>
#import "WZLBadgeImport.h"
#import "OPHomeUserLoginBtn.h"
#import <YYCategories/YYCategories.h>

@interface OPHomeUserLoginBtn()

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UILabel *textLabel;
@property (nonatomic,strong) UIImageView *indicatorImageView;
@property (nonatomic,strong) UIImageView *userAvatarImageView;

@end

@implementation OPHomeUserLoginBtn


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self makeSubviews];
        [self makeConstraints];
        [self updateViewsAfterLoginStatusChanged];
        [self updateNoticeRedDotStatus];
//        [self addObserver];
    }
    return self;
}

- (void)makeSubviews {
    [self addSubview:self.bgView];
    [self addSubview:self.userAvatarImageView];
    [self addSubview:self.textLabel];
    [self addSubview:self.indicatorImageView];
//    [self addSubview:self.redDotView];
}

- (void)makeConstraints {
    [self.userAvatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(32);
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userAvatarImageView.mas_right).offset(6);
        make.centerY.equalTo(self.userAvatarImageView.mas_centerY);
    }];
    
    [self.indicatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.textLabel.mas_centerY);
        make.left.equalTo(self.textLabel.mas_right).offset(0);
        make.right.mas_equalTo(-6);
        make.size.mas_equalTo(self.indicatorImageView.size);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.equalTo(self);
        make.right.equalTo(self.mas_right).offset(8);
    }];
    [self.textLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)updateViewsAfterLoginStatusChanged {
    self.backgroundColor = [UIColor clearColor];
    self.indicatorImageView.hidden = NO;
    if ([TYHLoginManager sharedInstance].isLogin) {
       // UIImage *placeholderImage = [UIImage imageNamed:@"home_nav_avatarplaceholder_icon"];
     //   [self.userAvatarImageView setImageWithPid:ZYBUser.sharedInstance.currentUser.avatar isBigImage:NO placeholderImage:placeholderImage];
    } else {
        self.userAvatarImageView.image = [UIImage imageNamed:@"home_navdefault_avatar_icon"];
    }
}

// 设置小红点的显示和隐藏
- (void)updateNoticeRedDotStatus {
    if ([TYHLoginManager sharedInstance].isLogin) {
        
        [self shouldHiddenNoticeRedDot]?[self.userAvatarImageView clearBadge]:[self.userAvatarImageView showBadge];
    } else {
        [[NSUserDefaults standardUserDefaults] boolForKey:@"feedLoginIconRedDotKey"]?[self.userAvatarImageView clearBadge]:[self.userAvatarImageView showBadge];
    }
}

// 已经登录时 判断是否需要显示红点提醒
- (BOOL)shouldHiddenNoticeRedDot {
   // TYHUserEntity *userInfo = [TYHLoginManager sharedInstance].currentUser;
//    if (!userInfo.bindPhone && (userInfo.phone.length == 0)) {
//        NSTimeInterval timestamp = [NSUserDefaults.standardUserDefaults doubleForKey:@"ZYBClickIconPhoneTimestamp"];
//        if ((NSDate.date.timeIntervalSince1970 - timestamp) > 604800) { // 距离上次点击间隔大于 7 天才显示
//            [NSUserDefaults.standardUserDefaults setDouble:-1 forKey:@"ZYBClickIconPhoneTimestamp"]; // 重置时间计算
//            return NO;
//        }
//    }
    return YES;
}

#pragma mark - Public Method

- (void)dismissRedDot {
//    self.redDotView.hidden = YES;
}

- (void)refreshBtn:(BOOL)hideBg {
    _textLabel.textColor = [UIColor colorWithHex:0x141414];
    _indicatorImageView.tintColor = [UIColor colorWithHex:0x000000];
}

#pragma mark - Event Action

- (void)clickAvatarAction {
    
    if ([[NSUserDefaults standardUserDefaults] doubleForKey:@"ZYBClickIconPhoneTimestamp"] < 0 ) {
        [[NSUserDefaults standardUserDefaults] setDouble:NSDate.date.timeIntervalSince1970 forKey:@"ZYBClickIconPhoneTimestamp"];
    }
//    self.redDotView.hidden = YES;
    [self.userAvatarImageView clearBadge];
    if (self.clickAvatar) {
        self.clickAvatar();
    }
}

#pragma mark - Getter Setter

- (UIImageView *)userAvatarImageView {
    if (!_userAvatarImageView) {
        _userAvatarImageView = [UIImageView new];
        _userAvatarImageView.image = [UIImage imageNamed:@"user_home_head_default_img"];
        _userAvatarImageView.layer.cornerRadius = 16;
        _userAvatarImageView.layer.masksToBounds = YES;
        _userAvatarImageView.userInteractionEnabled = YES;
        //_userAvatarImageView.backgroundColor = [UIColor colorWithHex:0xf7f9fa]; //t3
        
        UITapGestureRecognizer *tapAvatarImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAvatarAction)];
        [_userAvatarImageView addGestureRecognizer:tapAvatarImageView];
    }
    return _userAvatarImageView;
}

- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [UILabel new];
        _textLabel.textColor = [UIColor colorWithHex:0x141414];
        _textLabel.font = [UIFont systemFontOfSize:12.0f];
        _textLabel.text = @"登录/注册";
    }
    return _textLabel;
}

- (UIImageView *)indicatorImageView {
    if (!_indicatorImageView) {
        _indicatorImageView = [UIImageView new];
        //_indicatorImageView.image = [UIImage uxcImageWithName:@"icon_xznj"];
        _indicatorImageView.tintColor = [UIColor colorWithHex:0x000000];
        [_indicatorImageView sizeToFit];
        _indicatorImageView.hidden = YES;
    }
    return _indicatorImageView;
}

//- (UIView *)redDotView {
//    if (!_redDotView) {
//        _redDotView = [UIView new];
//        _redDotView.backgroundColor = [UIColor colorWithHex:0xF54327];
//        _redDotView.layer.cornerRadius = 3;
//    }
//    return _redDotView;
//}

- (UIView *)bgView {
    if (_bgView == nil) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.layer.cornerRadius = 12;
    }
    return _bgView;
}

@end
